<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Tintuc extends CI_Controller 
{
	
	public	function __construct()
	{
		parent::__construct();
		$this->load->library('pagination');
		if($this->session->userdata('user_id')){
			//$this->load->helper("url");
			$this->load->model("tintucs");
			$this->load->model("users");
			//$this->load->model("lich");
			date_default_timezone_set('UTC');
		}
		else{
			redirect('login');
		}
	}
	
	public function index()
	{
		//$this->viewAllTintuc();
		$this->viewPageTintuc(0);
	}
	public function viewAllTintuc()
	{		
		$data['allnew'] = $this->tintucs->getAllTintuc();
		$menu['menu'] = "thongbao";
		$this->load->view('header_block_v1',$menu);
		$this->load->view('content_sidebar_block',$menu);
		$this->load->view('tintuc/viewAllTintuc',$data);
		$this->load->view('footer_block');
	}
	public function viewPageTintuc($id=null)
	{
		$total_row = $this->tintucs->getSoluongTintuc();
		$data['allnew'] = $this->tintucs->getPage($id);
		$this->paging($total_row);
		$menu['menu'] = "thongbao";
		$this->load->view('header_block_v1',$menu);
		$this->load->view('content_sidebar_block',$menu);
		$this->load->view('tintuc/viewAllTintuc',$data);
		$this->load->view('footer_block');
	}
	public function viewTintuc($id_tintuc)
	{
		$data['new'] = $this->tintucs->getTintuc($id_tintuc);
		$menu['menu'] = "thongbao";
		$this->load->view('header_block_v1',$menu);
		$this->load->view('content_sidebar_block',$menu);
		$this->load->view('tintuc/viewTintuc',$data);
		$this->load->view('footer_block');
	}
	
	public function viewAddTintuc()
	{
		$quyen01 = $this->users->getQuyen($this->session->userdata("user_id"));
		if(count($quyen01) > 0){
			foreach($quyen01 as $q){
				$quyen[] = $q->id_quyen;
			}
		}
		else
			$quyen = array();
		if(in_array(3, $quyen)) {
			$menu['menu'] = 'addtin';
			$this->load->view('header_block_v1',$menu);
			$this->load->view('content_sidebar_block',$menu);
			$this->load->view('tintuc/viewAddTintuc');
			$this->load->view('footer_block');
		}
		else 
			redirect('tintuc');
	}
	public function addTintuc()
	{
		$this->load->library('form_validation');
		$this->form_validation->set_error_delimiters('<div class="error">', '</div>');
		//dieu kien form
		$this->form_validation->set_rules('tieude_tin', 'Tiêu đề thông báo', 'xss_clean|required|min_length[5]');
		$this->form_validation->set_rules('noidung_tin', 'Nội dung thông báo', 'xss_clean|required|min_length[5]');
		if ($this->form_validation->run() == FALSE){
			$this->viewAddTintuc();
		}
		else {
			$data["tieude_tintuc"] = $this->input->post("tieude_tin");
			$data["noidung_tintuc"] = $this->input->post("noidung_tin");
			$data["ngay_tintuc"] = date("Y-m-d H:i:s");
			$url = base_url()."tintuc";
			if($this->tintucs->insertTintuc($data)){
				echo "<script>alert('Bạn đã thêm 1 thông báo mới');	window.location='".$url."'; </script>";
			}
			else
				echo "<script>alert('Lỗi: Không insert được!');	window.location='".$url."'; </script>";
		}
	}
	public function viewEditTintuc($id_tintuc)
	{
		$quyen01 = $this->users->getQuyen($this->session->userdata("user_id"));
		if(count($quyen01) > 0){
			foreach($quyen01 as $q){
				$quyen[] = $q->id_quyen;
			}
		}
		else
			$quyen = array();
		if(in_array(3, $quyen)) {
			$data['tintuc'] = $this->tintucs->getTintuc($id_tintuc);
			$menu['menu'] = "";
			$this->load->view('header_block_v1',$menu);
			$this->load->view('content_sidebar_block',$menu);
			$this->load->view('tintuc/viewEditTintuc',$data);
			$this->load->view('footer_block');
		}
		else
			redirect('tintuc');
	}
	public function editTintuc($id_tintuc)
	{
		$this->load->library('form_validation');
		$this->form_validation->set_error_delimiters('<div class="error">', '</div>');
		//dieu kien form
		$this->form_validation->set_rules('tieude_tin', 'Tiêu đề thông báo', 'xss_clean|required|min_length[5]');
		$this->form_validation->set_rules('noidung_tin', 'Nội dung thông báo', 'xss_clean|required|min_length[5]');
		if ($this->form_validation->run() == FALSE){
			$this->viewEditTintuc($id_tintuc);
		}
		else {
			$data["tieude_tintuc"] = $this->input->post("tieude_tin");
			$data["noidung_tintuc"] = $this->input->post("noidung_tin");
			$data["ngay_tintuc"] = date("Y-m-d H:i:s");
			$url = base_url()."tintuc";
			if($this->tintucs->updateTintuc($data,$id_tintuc)){
				echo "<script>alert('Bạn đã sửa thành công');	window.location='".$url."'; </script>";
			}
			else
				echo "<script>alert('Lỗi: Không update được!');	window.location='".$url."'; </script>";
		}
	}
	public function delTintuc($id_tintuc)
	{
		$url = base_url()."tintuc";
		if($this->tintucs->delTintuc($id_tintuc)){
			echo "<script>alert('Bạn đã xóa thành công');	window.location='".$url."'; </script>";
		}
		else
			echo "<script>alert('Lỗi: Không del được!');	window.location='".$url."'; </script>";
	}
	
	function paging($total_row){
		$this->load->library('pagination');
		$config['base_url']=site_url("tintuc/viewPageTintuc");
		$config['total_rows']=$total_row;
		$config['per_page']=15;
		$config['full_tag_open'] = "<div class='news_pagination'>";
        $config['full_tag_close'] = '</div>';
        $config['first_link'] = 'Trang đầu';
        $config['first_tag_open'] = "<div class='news_page next'>";
        $config['first_tag_close'] = '</div>';
        $config['last_link'] = 'Trang cuối';
        $config['last_tag_open'] = "<div class='news_page next'>";
        $config['last_tag_close'] = '</div>';
        $config['next_link'] = 'Trang tiếp';
        $config['next_tag_open'] = "<div class='news_page next'>";
        $config['next_tag_close'] = '</div>';
        $config['prev_link'] = 'Trang trước';
        $config['prev_tag_open'] = "<div class='news_page next'>";
        $config['prev_tag_close'] = '</div>';
        $config['cur_tag_open'] = "<div class='news_page number curr_page'>";
        $config['cur_tag_close'] = '</div>';
        $config['num_tag_open'] = "<div class='news_page number'>";
        $config['num_tag_close'] = '</div>';
		$this->pagination->initialize($config);
	}
	
}